<template>
  <div>
    <el-form :model="form" inline>
      <el-form-item label="用户名：">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="分组：">
        <el-select v-model="form.groupId">
          <el-option label="全部" :value="-1" />
          <el-option :label="item.name" :value="item.id" v-for='item in dataTable' :key='item.id' />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库：">
        <el-select v-model="form.deptName">
          <el-option label="上海仓库" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
        <el-button type="primary" @click="$router.push('/home/everyView/GroupingList')"
          style='margin-left:30px'>分组列表</el-button>
      </el-form-item>
    </el-form>
  </div>
  <el-table ref="multipleTableRef" :data="tableData" border style="width:500px" max-height="650">
    <el-table-column label="用户名称" width="120">
      <template #default="scope">{{ scope.row.name }}</template>
    </el-table-column>
    <el-table-column label="所在仓库" width="120">
      <template #default="scope">{{ scope.row.deptName }}</template>
    </el-table-column>
    <el-table-column label="所属分组" width="120">
      <template #default="scope">{{ scope.row.groupName }}</template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button link type="primary" size="small" @click.prevent="setName(scope.row)">
          编辑
        </el-button>
      </template>
    </el-table-column>

  </el-table>

  <el-dialog v-model="dialogVisibleFZ" width="500">
    <span>
      <el-select v-model="dataTableForm.sysUserGroupId">
        <el-option :label="item.name" :value="item.id" v-for='item in dataTable' :key='item.id' />
      </el-select>
    </span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisibleFZ = false">取消</el-button>
        <el-button type="primary" @click="submintSet">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, onBeforeMount } from 'vue'
import { ElMessage } from 'element-plus'

import { sysUserGroupRelationlist, sysUserGrouplist, sysUserGroupRelationupdate } from '@/api/index'
// import { useRouter } from 'vue-router'
// const router = useRouter()
onBeforeMount(() => {
  getList()
  getListFenzu()
})
const form = ref({ groupId: -1 })
const onSubmit = () => {
  getList()
}
const getList = () => {
  sysUserGroupRelationlist(
    {
      name: form.value.name,
      groupId: form.value.groupId,
      deptName: form.value.deptName,
      token: localStorage.getItem('system_token')
    }
  ).then(res => {
    const { data: { data } } = res
    tableData.value = data
  })
}

// 分组
const dialogVisibleFZ = ref(false)
const tableData = ref([])
const setName = (row) => {
  console.log(row)
  if (row.groupId === 0) {
    dataTableForm.value.sysUserGroupId = ''
  } else {
    dataTableForm.value.sysUserGroupId = row.groupId
  }

  dataTableForm.value.userId = row.userId
  dataTableForm.value.userName = row.name
  dialogVisibleFZ.value = true
}
const dataTable = ref([])
const dataTableForm = ref({
  sysUserGroupId: '',
  userId: ''
})
const getListFenzu = () => {
  sysUserGrouplist().then(res => {
    const { data: { data } } = res
    dataTable.value = data
  })
}
const submintSet = () => {
  sysUserGroupRelationupdate(dataTableForm.value).then(res => {
    ElMessage.success('操作成功')
    getList()
    dialogVisibleFZ.value = false
  })
}
</script>

<style lang='less' scoped></style>
